﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace 文件读取与合并
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string ret = "";
            textBox1.Text = " ";
            textBox1.Text = "文件1的内容是：" + Environment.NewLine;
            StreamReader sr = new StreamReader("s1.txt", Encoding.Default);
            String line;
            while ((line = sr.ReadLine()) != null)
            {
                textBox1.Text+=line+ Environment.NewLine;
            }
            sr.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string ret = "";
            textBox1.Text = " ";
            textBox1.Text = "文件2的内容是：" + Environment.NewLine;
            StreamReader sr = new StreamReader("s2.txt", Encoding.Default);
            String line;
            while ((line = sr.ReadLine()) != null)
            {
                textBox1.Text += line + Environment.NewLine;
            }
            sr.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string ret = "";
            textBox1.Text = " ";
            textBox1.Text = "文件3的内容是：" + Environment.NewLine;
            StreamReader sr = new StreamReader("s3.txt", Encoding.Default);
            String line;
            while ((line = sr.ReadLine()) != null)
            {
                textBox1.Text += line + Environment.NewLine;
            }
            sr.Close();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            FileStream fs = new FileStream("s3.txt", FileMode.Open, FileAccess.Write);
            StreamWriter sw = new StreamWriter(fs);
            StreamReader sr1 = new StreamReader("s1.txt", Encoding.Default);
            StreamReader sr2 = new StreamReader("s2.txt", Encoding.Default);
            String line;
            while ((line = sr1.ReadLine()) != null)
            {
                sw.WriteLine(line);
            }
            while ((line = sr2.ReadLine()) != null)
            {
                sw.WriteLine(line);
            }
            sw.Close();
            fs.Close();
            textBox1.Text = "已经将文件1和文件2合并到文件3！" + Environment.NewLine;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            System.IO.File.WriteAllText(@"s3.txt", string.Empty);
            textBox1.Text = "文件3已清空！";
        }
    }
}
